package com.ruoyi.oa.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.oa.domain.OaHumanClockTotal;
import com.ruoyi.oa.domain.bo.AdjustClockBo;
import com.ruoyi.oa.domain.bo.ClockUserInfoBo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.vo.OaHumanClockVo;
import com.ruoyi.oa.domain.bo.OaHumanClockBo;
import com.ruoyi.oa.service.IOaHumanClockService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考勤规则
 *
 * @author ruoyi
 * @date 2023-07-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/HumanClock")
public class OaHumanClockController extends BaseController {

    private final IOaHumanClockService iOaHumanClockService;

    /**
     * 查询考勤规则列表
     */
//    @SaCheckPermission("oa:HumanClock:list")
    @GetMapping("/list")
    public TableDataInfo<OaHumanClockVo> list(OaHumanClockBo bo, PageQuery pageQuery) {
        return iOaHumanClockService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出考勤规则列表
     */
//    @SaCheckPermission("oa:HumanClock:export")
    @Log(title = "考勤规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaHumanClockBo bo, HttpServletResponse response) {
        List<OaHumanClockVo> list = iOaHumanClockService.queryList(bo);
        ExcelUtil.exportExcel(list, "考勤规则", OaHumanClockVo.class, response);
    }

    /**
     * 获取考勤规则详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("oa:HumanClock:query")
    @GetMapping("/{id}")
    public R<OaHumanClockVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaHumanClockService.queryById(id));
    }

    /**
     * 新增考勤规则
     */
//    @SaCheckPermission("oa:HumanClock:add")
    @Log(title = "考勤规则", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaHumanClockBo bo) {
        return toAjax(iOaHumanClockService.insertByBo(bo));
    }

    /**
     * 修改考勤规则
     */
//    @SaCheckPermission("oa:HumanClock:edit")
    @Log(title = "考勤规则", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaHumanClockBo bo) {
        return toAjax(iOaHumanClockService.updateByBo(bo));
    }


//    @SaCheckPermission("oa:HumanClock:edit")
    @PutMapping("/adjustClock")
    public R<Void> adjustClock(@RequestBody AdjustClockBo bo) throws ParseException {
        Date now=new Date();
        SimpleDateFormat holidayFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = holidayFormat.format(now);
        if (format.equals(bo.getReplaceDate())){
            return R.fail("不能对今天的打卡数据进行操作");
        }
        return toAjax(iOaHumanClockService.adjustClock(bo));
    }

    /**
     * 删除考勤规则
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("oa:HumanClock:remove")
    @Log(title = "考勤规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaHumanClockService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取用户补卡信息`
     * @param userId
     * @param month
     * @return
     */
    @Log(title = "考勤规则", businessType = BusinessType.DELETE)
    @GetMapping("/getUserReplaceInfo")
    public R<JSONObject> getUserReplaceInfo(@RequestParam(value = "userId", required = false) String userId,
                                         @RequestParam(value = "month", required = false) String month){
        return R.ok(iOaHumanClockService.getUserReplaceInfo(userId, month));
    }

    @SaIgnore
    @PutMapping("/clock")
    public R<Boolean> clock(@RequestBody ClockUserInfoBo bo) {
        return iOaHumanClockService.clock(bo);
    }

    @GetMapping("/getClockLocation")
    public R<Boolean> getClockLocation(@RequestParam(value = "userId", required = false) String userId,
                                       @RequestParam(value = "date", required = false)
                                      @DateTimeFormat(pattern = "yyyy-MM-dd")
                                      @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd")
                                      Date date,@RequestParam(value = "longitude", required = false)double longitude,@RequestParam(value = "latitude", required = false)double latitude){
        return iOaHumanClockService.getClockLocation(userId,date,longitude,latitude);
    }


    @GetMapping("/getAllDatesInMonth")
    public R<JSONArray> getAllDatesInMonth(@RequestParam(required = false, defaultValue = "") String userId,
                                           @RequestParam(required = false, defaultValue = "") String yearMonth){
        return R.ok(iOaHumanClockService.getAllDatesInMonth(yearMonth,userId));
    }

    @GetMapping("/SelectNormalUserByMonth")
    public R<Map<String, Object>> SelectNormalUserByMonth(@RequestParam(required = false, defaultValue = "") String month){
       return R.ok(iOaHumanClockService.SelectNormalUserByMonth(month));
    }

    @GetMapping("/getWorkOrOffClock")
    public R<String> getWorkOrOffClock(@RequestParam(value = "clockDate", required = false)
                                                    @DateTimeFormat(pattern = "HH:mm")
                                                    @JsonFormat(locale="zh", timezone="GMT+8", pattern="HH:mm")
                                                    Date clockDate,@RequestParam(required = false, defaultValue = "") String userId){
        return R.ok(iOaHumanClockService.getWorkOrOffClock(clockDate,userId));
    }

    @GetMapping("/SelectNormalUserByDate")
    public R<Map<String, Object>> SelectNormalUserByDate(@RequestParam(required = false, defaultValue = "")
                                                    @DateTimeFormat(pattern = "yyyy-MM-dd")
                                                    @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd")
                                                    Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String format = sdf.format(date);
        return R.ok(iOaHumanClockService.SelectNormalUserByDate(format));
    }

    @GetMapping("/getAllShouldClockNum")
    public R<Integer> getAllShouldClockNum(){
        return R.ok(iOaHumanClockService.getAllShouldClockNum());
    }
}
